const modalPlugin = requirePlugin('xlightPlugin');
Component({
	data: {
		interstitialAd: {},
		oder: "",
		a: "",
		phone: "",
		src: "",
		param: "",
		nomobile: '',
		orderNo:'',
		spaceCode:''
	},
	onInit() {

		console.log(my.getStorageSync({
			key: 'orderNo'
		}).data,'获取订单')
		const ad = new modalPlugin.CreateInterstitialAd();
		this.setData({
			interstitialAd: ad,
		})
		if (my.getStorageSync({
				key: 'phone'
			}).data) {
			this.setData({
				phone: my.getStorageSync({
					key: 'phone'
				}).data
			})
		}
		if (my.getStorageSync({
				key: 'a'
			}).data) {
			this.setData({
				a: my.getStorageSync({
					key: 'a'
				}).data
			})
		}
		if (my.getStorageSync({
				key: 'param'
			}).data) {
			this.setData({
				param: my.getStorageSync({
					key: 'param'
				}).data
			})
		}

		
		//如果有订单号就计数，没有就生成
		if(my.getStorageSync({
			key: 'deviceid'
		}).data.length>20){
			my.request({
			  url: 'https://www.sx2b2c.com/api/rf/getMpUrl', // 请求的 URL
			  method: 'POST', // POST 方法
			  data: {
				env:34,
				a:my.getStorageSync({
					key: 'a'
				}).data
			  },
			  header: {
			    'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
			  },
			  success:(result)=> {
						
			  },
			  fail: function() {	
			  }
			})
		}
	
	if(my.getStorageSync({
			key: 'orderNo'
		}).data){
	
		this.setData({
			orderNo:my.getStorageSync({
			key: 'orderNo'
		}).data
		})
	my.request({
	  url: 'https://www.sx2b2c.com/api/Alipayad/addNum', // 请求的 URL
	  method: 'POST', // POST 方法
	  data: {
		orderNo:this.data.orderNo
	  },
	  header: {
	    'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
	  },
	  success:(result)=> {
	
	  },
	  fail: function() {
	
	  }
	})
	}else{
	
		var a = new Date().getFullYear() + (new Date().getMonth() + 1).toString().padStart(2, "0") +
				new Date().getDate().toString().padStart(2, "0")
			//  "2020-02-12"   获取的是当前日期
			var b = new Date().getHours().toString().padStart(2, "0") + new Date().getMinutes().toString()
				.padStart(2, "0") + new Date().getSeconds().toString().padStart(2, "0")
			//11:13:02当前时间
		
			var randomNum = "" + Math.round(Math.random() * 900000000);
			while (randomNum.length < 8) {
				randomNum = "0" + randomNum;
			}
		
		
			const number = 'AD' + a + b + randomNum
		
		this.setData({
				orderNo:number
		})
			
	
			my.setStorageSync({
				key: 'orderNo',
				data: this.data.orderNo
			});

		my.request({
		  url: 'https://www.sx2b2c.com/api/Alipayad/subOrder', // 请求的 URL
		  method: 'POST', // POST 方法
		  data: {
		    a: this.data.a, 
		    param:this.data.param,
			mobile:this.data.phone,
			nomobile:'1',
			from_t:'3',
			from_env:"0",
			orderNo:this.data.orderNo
		  },
		  header: {
		    'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
		  },
		  success:(res)=> {
			 my.request({
			   url: 'https://www.sx2b2c.com/api/Alipayad/addNum', // 请求的 URL
			   method: 'POST', // POST 方法
			   data: {
			 	orderNo:this.data.orderNo
			   },
			   header: {
			     'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
			   },
			   success:(result)=> {
			
			   },
			   fail: function() {
			 
			   }
			 })

		  
			
		  },
		  fail: function(res) {
		    
		  }
		})
	}

		this.data.interstitialAd.onLoad(() => {
	
			my.request({
			  url: 'https://www.sx2b2c.com/api/Alipayad/getSpaceCode', // 请求的 URL
			  method: 'POST', // POST 方法
			  data: {
				a:this.data.a,
				xcx:3,
				param:this.data.param
			  },
			  header: {
			    'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
			  },
			  success:(result)=> {
			console.log(result,'99999999999999999')
			my.request({
			  url: 'https://www.sx2b2c.com/api/rf/addLog', // 请求的 URL
			  method: 'POST', // POST 方法
			  data: {
				v1:'code='+result.data.data.spaceCode+'a='+this.data.a+'orderNo='+this.data.orderNo
			  },
			  header: {
			    'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
			  },
			
			})
			this.setData({
				spaceCode: result.data.data.spaceCode
			})
			my.request({
			  url: 'https://www.sx2b2c.com/api/Alipayad/log', // 请求的 URL
			  method: 'POST', // POST 方法
			  data: {
			a: my.getStorageSync({
				key: 'a'
			}).data,
			eventType: 3,
			xcx: 3,
			deviceid: my.getStorageSync({
				key:'deviceid'
			}).data,
			spaceCode: result.data.data.spaceCode
			  },
			  header: {
			    'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
			  },
			  success:(result)=> {
				
			  },
			  fail: function() {	
			  }
			})
		
			this.data.interstitialAd.show({
				spaceCode:this.data.spaceCode,
				rtaExtMap: {
					param: this.data.param,
					a: this.data.a,
					mobile: this.data.phone,
					orderNo: this.data.orderNo,
					organization_type: 'hospital',
					organization_sub_type: 'general_hospital',
					device_location: 'outpatient_department',
					device_id: '111111',
					public_opinion_level: 'low_risk',
					touch_point_location: 'index',
					touch_point_type: 'full_screen',
				}
			})
			  },
			  fail: function(err) {
			console.log(err)
			  }
			})
		
		})

		this.data.interstitialAd.onClose((isClick) => {
			let link = "https://render.alipay.com/p/s/i/?scheme=" + encodeURIComponent(
					"alipays://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&qrcode=") +
				encodeURIComponent('https://kgao.yuantiaozhe.net/advertisement/white.html?appCode=22756&type=6')
			my.ap.navigateToAlipayPage({
				path: link
			})
			console.log('关闭啊啊啊啊啊啊', isClick);
		});

		this.data.interstitialAd.onError((err) => {
			console.log('error', err);
			//报错统计
			my.request({
			  url: 'https://www.sx2b2c.com/api/Alipayad/iserror', // 请求的 URL
			  method: 'POST', // POST 方法
			  data: {
				orderNo:this.data.orderNo
			  },
			  header: {
			    'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
			  },
			  success:(result)=> {
						
			  },
			  fail: function() {	
			  }
			})
			//错误跳转地址
			let url = encodeURIComponent(my.getStorageSync({
				key: 'url'
			}).data)
			if (url != '') {

				let link = "https://render.alipay.com/p/s/i/?scheme=" + encodeURIComponent(
						"alipays://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&qrcode="
						) +
					encodeURIComponent(url)
				my.ap.navigateToAlipayPage({
					path: link
				})
			}else{
			
			}


		});
	}, 
	methods: {
		
	},
});